package com.graduation.rescuematerial.controller.system;

import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/29 15:40
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "系统管理员——管理全部用户")
public class SysUserController {

    @Autowired
    private UserService userService;

    @ApiOperation("联合查询平台所有成员")
    @ResponseBody
    @PostMapping("/union/query")
    public RespBean unionQuery(@RequestBody UnionQueryUserVo unionQueryUserVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=unionQueryUserVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return userService.sysUnionQuery(unionQueryUserVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看，检查是否登陆过期");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("/注销用户")
    @PostMapping("/cancel")
    @ResponseBody
    public RespBean cancelUser(@RequestBody CancelUserVo cancelUserVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=cancelUserVo.getTicket();
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo ){
                //获取登录用户的手机号，根据手机号作修改
                String action=userVo.getAction();
                if((action.equals("system_administrator")|| action.equals("organization_administrator"))){
                    //companyName非root时只能修改本部门的信息
                    return userService.cancelUser(cancelUserVo.getPhone(),cancelUserVo.getStatus(),userVo.getCompanyName());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"无操作权限");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"未登录");
    }


}
